package com.xtaller.easy.admin.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.common.convert.J;
import com.xtaller.common.convert.V;
import com.xtaller.easy.admin.base.TApi;
import com.xtaller.easy.admin.config.R;
import com.xtaller.easy.admin.model.SysButton;
import com.xtaller.easy.admin.service.impl.SysButtonService;
import com.xtaller.easy.admin.specification.ButtonCreate;
import com.xtaller.easy.admin.specification.ButtonUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Taller on 2018/4/16
 */
@Api(tags = "按钮权限api")
@RestController
@RequestMapping("/v1/sys")
@CrossOrigin   //跨域服务注解
public class ButtonApi extends TApi {
    @Autowired
    private SysButtonService buttonService;

    private Map<String,String> ButtonVerify(){
        Map<String,String> verify = new HashMap<>();
        verify.put("fun","前端执行函数不能为空");
        verify.put("name","按钮名称不能为空");
        return verify;
    }

    @GetMapping("/button")
    @ApiOperation(value = "读取按钮数据列表")
    public Object getButton(){
        List<SysButton> buttons = buttonService.getButton();
        return R.ok(data2Grid(buttons.size(), buttons));
    }

    @PostMapping("/button")
    @ApiOperation(value = "创建权限按钮")
    public Object createButton(@RequestBody ButtonCreate object){
        JSONObject check = V.checkEmpty(ButtonVerify(), object);
        if(check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysButton button = J.o2m(object, SysButton.class);
        button.setCreator(getUserIdByToken());
        // 名称唯一性判断
        if(buttonService.existName(button, true))
            return R.error("权限按钮名称已经存在请更换");
        // 函数唯一性判断
        if(buttonService.existFun(button, true))
            return R.error("权限按钮对应函数已经存在请更换");
        button = buttonService.createSysButton(button);
        if(button == null)
            return R.error("创建权限按钮失败,请稍后再试");
        return R.ok("创建权限按钮成功", formatObject(button));
    }

    @PutMapping("/button")
    @ApiOperation(value = "修改权限按钮")
    public Object updateButton(@RequestBody ButtonUpdate object){
        if(object.getId() == null)
            return R.error("数据Id不能为空");
        if(buttonService.selectById(object.getId()) == null)
            return R.error("数据Id异常");
        JSONObject check = V.checkEmpty(ButtonVerify(), object);
        if(check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysButton button = J.o2m(object, SysButton.class);
        button.setReviser(getUserIdByToken());
        // 名称唯一性判断
        if(buttonService.existName(button, false))
            return R.error("权限按钮名称已经存在请更换");
        // 函数唯一性判断
        if(buttonService.existFun(button, false))
            return R.error("权限按钮对应函数已经存在请更换");
        button = buttonService.updateSysButton(button);
        if(button == null)
            return R.error("修改权限按钮失败,请稍后再试");
        return R.ok("修改权限按钮成功", formatObject(button));
    }

    @DeleteMapping("/button/{id}")
    @ApiOperation(value = "删除字典模块")
    public Object deleteButton(@PathVariable("id") String id){
        if(buttonService.selectById(id) == null)
            return R.error("数据Id异常");
        if(buttonService.deleteSysButton(id, getUserIdByToken()))
            return R.ok("字典模块删除成功");
        return R.error("删除失败请稍后再试");
    }
}
